package com.sykj.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.sykj.bean.bis.BisCustomUser;
import com.sykj.config.oss.FileUploadUtil;
import com.sykj.mapper.BisCustomUserMapper;
import com.sykj.service.BisCustomUserService;
import com.sykj.util.file.FileUploadResult;
import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @program: BeautyClub
 * @description: 用户信息接口
 * @author: qiaoli
 * @create: 2020-07-25 16:26
 */
@Service
public class BisCustomUserServiceImpl  extends ServiceImpl<BisCustomUserMapper, BisCustomUser> implements BisCustomUserService {
@Autowired
FileUploadUtil fileUploadUtil;

    @Override
    public ResultBody addWechatMessage(String tel, String openid) {
        if(tel!=null){

         String phone=tel;
         UpdateWrapper updateWrapper=new UpdateWrapper();
         updateWrapper.eq("openid",openid);
         updateWrapper.set("mobile",phone);
         
          this.baseMapper.update(null,updateWrapper);
          QueryWrapper queryWrapper= new QueryWrapper();
          queryWrapper.eq("openid",openid);
            BisCustomUser user = this.baseMapper.selectOne(queryWrapper);
            return  new ResultBody(CommonEnum.SUCCESS,user);
        }
        return new ResultBody(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    @Override
    public ResultBody updateUser(BisCustomUser bisCustomUser) {
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("custom_user_id",bisCustomUser.getCustomUserId());
        updateWrapper.set("sex",bisCustomUser.getSex());
        updateWrapper.set("mobile",bisCustomUser.getMobile());
        updateWrapper.set("username",bisCustomUser.getUsername());
        updateWrapper.set("born_date",bisCustomUser.getBornDate());
        int update = this.baseMapper.update(null, updateWrapper);
        if(update>0){
            BisCustomUser bisCustomUser1 = this.baseMapper.selectById(bisCustomUser.getCustomUserId());
            return  new ResultBody(CommonEnum.SUCCESS, bisCustomUser1);
        }
        return null;
    }

    @Override
    public ResultBody updateImg(MultipartFile file,String customUserId) {

        if (null != file) {
            String filename = file.getOriginalFilename();
            if (!"".equals(filename.trim())) {
                //上传到OSS

                FileUploadResult upload = fileUploadUtil.upload(file);
                QueryWrapper queryWrapper=new QueryWrapper();
                queryWrapper.eq("custom_user_id",customUserId);
                BisCustomUser bisCustomUser = this.baseMapper.selectOne(queryWrapper);
                System.err.println(bisCustomUser);
                String photo = bisCustomUser.getPhoto();
                //判断是服务器否有这个图片，有就删除



                UpdateWrapper updateWrapper = new UpdateWrapper();
                updateWrapper.eq("custom_user_id", customUserId);
                updateWrapper.set("photo", upload.getName());
                int update = this.baseMapper.update(null, updateWrapper);
                System.err.println(upload.toString());
                boolean delete = fileUploadUtil.delete(photo);
                System.out.println(delete);
                if (update != 0) {

                    BisCustomUser bisCustomUser1 = this.baseMapper.selectOne(queryWrapper);
                    System.err.println(bisCustomUser1);

                    return new ResultBody(CommonEnum.SUCCESS, bisCustomUser1);
                } else {
                    return  new ResultBody(CommonEnum.NOT_FOUND);
                }



            }

        }
        return     new ResultBody(CommonEnum.NOT_FOUND);


    }


}

